<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\Admin\UsersCodeRequest;
use App\Http\Controllers\Controller;
use App\Models\Merchant;
use App\Models\UsersCode;

class UsersCodeController extends Controller
{
    protected $model;

    public function __construct(UsersCode $userscode)
    {
        $this->model = $userscode;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


    public function index(Request $request, UsersCode $userscode)
    {
        //
        $uscode = $userscode->with(['merchant'])->filter($request->all())->paginate(config('api.page.limit'));
        $customer = $request->customerNum;
        return view('admins.userscode.index',compact('uscode','customer'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $merchanrRes = Merchant::all();
        return view('admins.userscode.add')->with('merchanrRes', $merchanrRes);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

    public function store(Request $request,UsersCode $userscode)
    {
        $result = $userscode->create($request->all());
        if(!$result->id){
            return back()->with('warning','添加失败');
         }else{
             return redirect()->route('admin.userscode.index')->with('warning','添加成功！');
         }
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $result = UsersCode::find($id);
        $merchanrRes = Merchant::all();
        return view('admins.userscode.edit', compact('merchanrRes', 'result'));
        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    public function update(Request $request)
    {
        
        $result = UsersCode::find($request->input('id'));
        $result->merchant_id = $request->input('merchant_id');
        $result->merchant_name = $request->input('customerNum');
        $result->merchant_address = $request->input('shopNum');
        $result->telephone = $request->input('accesskey');
        $result->wechat = $request->input('secretkey');
        if($result->save()){
            return redirect()->route('admin.userscode.index')->with('success', '编辑成功');
        }else{
            return back()->with('warning','编辑失败');
        }
    }
    

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        if ($id) {
            $result = $this->model->where('id', $id);
        }
        return redirect()->route('admin.userscode.index');
    }
}
